package com.usermanage.controller;

import com.change.Service.AirSoftService;
import com.contract.Bean.TablePriceChgDtl;
import com.contract.Bean.TablePriceChgSmy;
import com.contract.Common.SkuPriceTableIOC;
import com.contract.Common.TablePriceChgIOC;
import com.contract.Service.TablePriceChgDtlService;
import com.contract.Service.TablePriceChgSmyService;
import com.ordercore.Util.ResponseData.ResponseData;
import com.ordercore.Util.Verify.pzDataVerifyUtil;
import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.Service.MessageService;
import com.usermanage.beanParam.PermissionBean;
import com.usermanage.util.StaticUtil;
import com.usermanage.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tariffModifyController")
public class TariffModifyController {
    @Resource
    private MessageService messageService;

    @RequestMapping("/list.page")
    public ModelAndView listPage() {
        ModelAndView mv = new ModelAndView();
        //获得用户登陆信息
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        //从用户登陆信息中获取用户权限列表
        List<PermissionBean> permissionBeanList = userDetail.getList();
        for (PermissionBean bean : permissionBeanList) {
            if (bean.getPermission_url().equals("tariffModifyConfirm.json")) {
                mv.addObject("confirm", true);
            }
            if (bean.getPermission_url().equals("tariffModifyRefuse.json")) {
                mv.addObject("refuse", true);
            }
            if (bean.getPermission_url().equals("tariffModifyDel.json")) {
                mv.addObject("del", true);
            }
        }
        mv.setViewName("/tariffModify/list");
        return mv;
    }


    @RequestMapping(value = "/detail.page")
    public ModelAndView detailPage(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id", id);
        mv.setViewName("/tariffModify/detail");
        return mv;
    }

    @RequestMapping(value = "/tablePriceChgSmyList.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseData tablePriceChgSmyList(int page, int limit, String key) {
        return TablePriceChgIOC.tablePriceChgSmyList(page, limit, key);
    }

    @RequestMapping(value = "/confirm.json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResponseData confirm(String id, String stat, String tariffId) throws Exception {
        Integer smy_id1 = null;
        if (pzDataVerifyUtil.isNotBlank(id)) {
            smy_id1 = Integer.valueOf(id);
        } else {
            return ResponseData.fail("未知的价目表");
        }
        if (pzDataVerifyUtil.isBlank(stat)) {
            return ResponseData.fail("未知的操作");
        }
        if ("del".equals(stat)) {
            return TablePriceChgIOC.tablePriceChgSmyDel(smy_id1);
        } else if ("00".equals(stat)) {
            if (StaticUtil.JDTARIFFID == Integer.valueOf(tariffId)) {
                ResponseData responseData = TablePriceChgIOC.tablePriceChgList(0, 0, "", Integer.valueOf(id));
                if (responseData.isRet()) {
                    List<Map> list = (List<Map>) responseData.getData();
                    async(list);
                }
            }
            return TablePriceChgIOC.confirm(smy_id1, stat);
        } else {
            return TablePriceChgIOC.confirm(smy_id1, stat);
        }

    }

    @RequestMapping(value = "/tablePriceChgList.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseData tablePriceChgList(int page, int limit, String key, String smy_id) {
        Integer smy_id1 = null;
        if (pzDataVerifyUtil.isNotBlank(smy_id)) {
            smy_id1 = Integer.valueOf(smy_id);
        }
        return TablePriceChgIOC.tablePriceChgList(page, limit, key, smy_id1);
    }

    @RequestMapping(value = "/tablePriceChgSmyDtl.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseData tablePriceChgSmyDtl(String id) {

        if (pzDataVerifyUtil.isBlank(id)) {
            ResponseData.fail("查询详情ID不能为空！");
        }

        TablePriceChgSmy tablePriceChgSmy = new TablePriceChgSmy();
        tablePriceChgSmy.setId(Integer.parseInt(id));
        return TablePriceChgIOC.tablePriceChgSmyDtl(tablePriceChgSmy);
    }


    private void async(List<Map> list) throws Exception {

        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            switch (map.get("opr").toString()) {
                case "00"://新上架
                    map.put("type", "add");
                    map.put("oprType", "010");
                    messageService.rabbitMq("syncChange", "syncKey", StringUtil.ObjectToJson(map));
                    break;
                case "02"://已更新
                    map.put("type", "update");
                    map.put("oprType", "030");
                    map.put("air_soft_id", map.get("sku_id"));
                    messageService.rabbitMq("syncChange", "syncKey", StringUtil.ObjectToJson(map));
                    break;
                case "01"://已下架
                    map.put("type", "del");
                    map.put("oprType", "020");
                    map.put("air_soft_id", map.get("sku_id"));
                    messageService.rabbitMq("syncChange", "syncKey", StringUtil.ObjectToJson(map));
                    break;
                default:
                    break;
            }
        }

    }
}
